package com.gzx.plugin.tjzy.modular.founds.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzx.plugin.tjzy.modular.founds.bo.GzxFoundsAmountBo;
import com.gzx.plugin.tjzy.modular.founds.bo.GzxFoundsFlowAddBo;
import com.gzx.plugin.tjzy.modular.founds.bo.GzxFoundsFlowQueryBo;
import com.gzx.plugin.tjzy.modular.founds.constant.FoundsConstant;
import com.gzx.plugin.tjzy.modular.founds.service.GzxFoundsFlowService;
import com.gzx.plugin.tjzy.modular.founds.vo.GzxFoundsAvailableVo;
import com.gzx.plugin.tjzy.modular.founds.vo.GzxFoundsFlowVo;
import com.gzx.plugin.tjzy.modular.project.entity.vo.GzxProjectVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.io.IOException;
import java.util.List;

/**
 * 充值电话短信流水记录 gzx_founds_flow_record
 *
 * @author xbl
 * &#064;date  2025-03-19
 */
@Slf4j
@RestController
@RequestMapping("/tjzy/founds")
@RequiredArgsConstructor
public class GzxFoundsFlowController {

    private final GzxFoundsFlowService foundsFlowService;

    /**
     * 根据项目代码获取项目的可用余额
     */
    @GetMapping("/getAvaliableByCode/{projectCode}")
    public CommonResult<GzxFoundsAvailableVo> getAvaliableByCode(@PathVariable("projectCode") String projectCode) {
        return CommonResult.data(foundsFlowService.getAvaliableByCode(projectCode));
    }

    /**
     * 充值流水记录：文件上传
     */
    @CommonLog("充值流水记录：文件上传")
    @PostMapping("/uploadFoundsFlow")
    public CommonResult<GzxProjectVo> uploadFoundsFlow(@RequestPart("file") MultipartFile file, @RequestPart("founds") GzxFoundsFlowAddBo bo) {
        try {
            if (null == file) {
                return CommonResult.error("请上传文件");
            }
            if (file.getSize() > FoundsConstant.FILE_SIZE) { // 限制文件大小为16MB
                return CommonResult.error("文件大小不能超过16MB");
            }
            GzxProjectVo projectVo = foundsFlowService.transRecharge(file, bo);
            return CommonResult.data(projectVo);
        } catch (IOException e) {
            return CommonResult.error("充值流水记录：文件上传 系统异常，请稍后重试");
        }
    }

    /**
     * 根据id查询流水记录
     */
    @GetMapping("/detailFoundsFlow/{id}")
    public CommonResult<GzxFoundsFlowVo> detailFoundsFlow(@PathVariable("id") String id) {
        return CommonResult.data(foundsFlowService.detailFoundsFlow(id));
    }

    /**
     * 查看某个产品的短信花了多少钱
     */
    @PostMapping("/productAmount")
    public CommonResult<List<GzxFoundsFlowVo>> productAmount(@RequestBody GzxFoundsAmountBo foundsAmountBo) {
        return CommonResult.data(foundsFlowService.productAmount(foundsAmountBo));
    }

    /**
     * 充值电话短信流水记录
     */
    @CommonLog("充值电话短信流水记录")
    @PostMapping("/pageFoundsFlow")
    public CommonResult<Page<GzxFoundsFlowVo>> pageFoundsFlow(@RequestBody GzxFoundsFlowQueryBo foundsFlowQueryBo) {
        return CommonResult.data(foundsFlowService.page(foundsFlowQueryBo));
    }

}
